package com.cx;

import com.cx.entity.LolBattleChampionEntity;
import com.cx.entity.LolHeaderJoinTeamEntity;
import com.cx.entity.LolTeamJoinChampionEntity;
import com.cx.po.LolBattleHeaderPo;
import com.cx.po.LolBattleTeamPo;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.LookupOperation;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 残星
 * @Description Mongo课题 测试类
 * @date 2022-11-30
 */
@Slf4j
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class MongoSubjectTestApplication {
    @Resource(name = "mongoTemplate")
    private MongoTemplate mongoTemplate;

    @Test
    public void testLolJoinAgg() {
        LookupOperation headerJoinTeam = LookupOperation.newLookup()
                //关联从表名
                .from("lol_battle_team")
                //主表关联字段
                .localField("gameId")
                //从表关联的字段
                .foreignField("gameId")
                //查询结果名
                .as("lolBattleTeamList");
        LookupOperation teamJoinChampion = LookupOperation.newLookup()
                //关联从表名
                .from("lol_battle_champion")
                //主表关联字段
                .localField("gameId")
                //从表关联的字段
                .foreignField("gameId")
                //查询结果名
                .as("lolBattleChampionList");
        Aggregation headerJoinTeamJoinChampionAgg = Aggregation.newAggregation(headerJoinTeam, teamJoinChampion);
        AggregationResults<LolHeaderJoinTeamEntity> headerJoinTeamEntityAggResults =
                mongoTemplate.aggregate(headerJoinTeamJoinChampionAgg, LolBattleHeaderPo.class, LolHeaderJoinTeamEntity.class);
        List<LolHeaderJoinTeamEntity> mappedResults = headerJoinTeamEntityAggResults.getMappedResults();
        // 这里需要将对局英雄信息，根据队伍（蓝色方/红色方）拆分后，再组装到对应的队伍信息下
        List<LolHeaderJoinTeamEntity> resultList = mappedResults.stream().peek(lolHeaderJoinTeamEntity -> {
            // 蓝色方
            List<LolBattleChampionEntity> blue = Optional.ofNullable(
                    lolHeaderJoinTeamEntity.getLolBattleTeamList()
                            .get(0)
                            .getLolBattleChampionList())
                    .orElseGet(ArrayList::new);
            // 红色方
            List<LolBattleChampionEntity> red = Optional.ofNullable(
                    lolHeaderJoinTeamEntity.getLolBattleTeamList()
                            .get(1)
                            .getLolBattleChampionList())
                    .orElseGet(ArrayList::new);
            List<LolBattleChampionEntity> lolBattleChampionList = lolHeaderJoinTeamEntity.getLolBattleChampionList();
            for (LolBattleChampionEntity lolBattleChampionEntity : lolBattleChampionList) {
                // 判断如果是红色方则加入red，否则加入blue
                if ("红色方".equals(lolBattleChampionEntity.getTeam())) {
                    red.add(lolBattleChampionEntity);
                } else {
                    blue.add(lolBattleChampionEntity);
                }
            }
            lolBattleChampionList.clear();
        }).collect(Collectors.toList());
    }

    @Test
    public void testLolJoinAgg2() {
        LookupOperation teamJoinChampion = LookupOperation.newLookup()
                //关联从表名
                .from("lol_battle_champion")
                //主表关联字段
                .localField("gameId")
                //从表关联的字段
                .foreignField("gameId")
                //查询结果名
                .as("lolBattleChampionList");
        Aggregation headerJoinTeamJoinChampionAgg = Aggregation.newAggregation(teamJoinChampion);
        AggregationResults<LolTeamJoinChampionEntity> teamJoinChampionEntityAggResults =
                mongoTemplate.aggregate(headerJoinTeamJoinChampionAgg, LolBattleTeamPo.class, LolTeamJoinChampionEntity.class);
        List<LolTeamJoinChampionEntity> mappedResults = teamJoinChampionEntityAggResults.getMappedResults();
        System.out.println("sss");
    }

    @Test
    public void testLolJoinAgg3() {
        LookupOperation headerJoinTeam = LookupOperation.newLookup()
                //关联从表名
                .from("lol_battle_team")
                //主表关联字段
                .localField("gameId")
                //从表关联的字段
                .foreignField("gameId")
                //查询结果名
                .as("lolBattleTeamList");
    }
}
